﻿using BLL;
using IBLL;
using kang.Entitys.IdentityAuthentication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using webMvc.Models.ViewModel;

namespace webMvc.Controllers
{
    public class RolesController : BaseController
    {
		IRoleInfoService roleInfoService = new RoleInfoService();
		IRoleActionMapperService roleActionMapperService = new RoleActionMapperService();
		public JsonResult AddRoleData(AddRoleViewModel addRoleViewModel)
		{
			RoleInfo roleInfo = roleInfoService.AddDonator(new RoleInfo()
			{
				RoleName = addRoleViewModel.roleName,
				Modifdon = addRoleViewModel.roleKey,
				Remark = addRoleViewModel.remark,
				DelFlag = addRoleViewModel.status == 1 ? true : false,
				Sort = addRoleViewModel.roleSort,
				SubTime = DateTime.Now
			});
			roleInfoService.CurrentDbSession.allSaveChangge();
			if (roleInfo != null)
			{
				foreach (string item in addRoleViewModel.menuIds.Split(','))
				{
					roleActionMapperService.AddDonator(new RoleActionMapper() { RoleId = roleInfo.Id, AactionId = int.Parse(item) });
				}
				//roleActionMapperService.AddDonator()
			}
			roleActionMapperService.CurrentDbSession.allSaveChangge();
			return Json(new MyJsonResult() { code = 0, msg = "新增成功!" });

		}

		public ActionResult AddRole()
		{
			return View();
		}

		public JsonResult GetRolesDatas(BaseSearchParameter SearchParameter)
		{
			TableResult<RoleInfo> tableResult = new TableResult<RoleInfo>();
			tableResult.rows = roleInfoService.LoadPageEntities(SearchParameter.pageNum, SearchParameter.pageSize, out tableResult.total, x => true, x => x.SubTime, true).ToList();
			return Json(tableResult, JsonRequestBehavior.AllowGet);
		}

		// GET: Roles
		public ActionResult Index()
        {
            return View();
        }
    }
}